package com.codegenerate.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codegenerate.demo.config.R;
import com.codegenerate.demo.entity.SysDictionary;
import com.codegenerate.demo.mapper.SysDictionaryMapper;
import com.codegenerate.demo.service.SysDictionaryService;
import com.codegenerate.demo.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class SysDictionaryServiceImpl extends ServiceImpl<SysDictionaryMapper, SysDictionary>
        implements SysDictionaryService {



    @Override
    public R<Long> saveForValidate(SysDictionary entity){
        //region:  校验
        if(entity==null){
            return R.failed("entity 不能为空");

        }
        if(StringUtils.isEmpty(entity.getCode())){
            return R.failed("getCode 不能为空");
        }
        var query = new QueryWrapper<SysDictionary>().lambda();
        query.eq(SysDictionary::getCode, entity.getCode());
        var pageResult = this.page(new Page<SysDictionary>(1, 1), query);
         if(pageResult!=null&&pageResult.getRecords().size()>=1){
             return R.failed(StringUtils.format("code:{} 已存在", entity.getCode()));
         }
        //endregion

        //region:  保存
        var isyes=this.save(entity);
         if(!isyes){
             return R.failed("新增失败");
         }
        return R.ok(entity.getId());
        //endregion
    };


}

